﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UniMana.Data
{
    public class System_User_RuleData
    {
        public static IQueryable GetSystemUserRuleByRef(DBMain data, Guid gidUserKey, int intGroupKey)
        {
            var iQuery = from sur in data.m_DCReadOnly.System_User_Rules
                         from sug in data.m_DCReadOnly.System_User_Groups
                         from sgr in data.m_DCReadOnly.System_Group_Rules
                         where sur.System_UserKey == gidUserKey &&
                         sug.System_GroupKey ==intGroupKey&&
                         sug.System_GroupKey == sgr.System_GroupKey &&
                         sgr.System_RuleKey == sur.System_RuleKey
                         select sur;

            return iQuery;
        }

        public static System_User_Rule GetCurrentRuleOfCurrentUser(DBMain data, string strRuleID)
        {
            System_User_Rule sur =  (from p in data.m_DCReadOnly.System_Rules
                                   from p1 in data.m_DCReadOnly.System_User_Rules
                                   where p1.System_RuleKey == p.System_RuleKey
                                   && p1.System_UserKey == SysGlobalVar.g_sta_sysUserLogin.Properties.System_UserKey
                                   && p.RuleID == strRuleID
                                   select p1).FirstOrDefault();
            return sur;
        }
       
        public static IQueryable<System_User_Rule> GetSystemUserRuleByUser(Guid _guiUserKey)
        {
            DBMain data = new DBMain();
            IQueryable<System_User_Rule> iQuery = from sur in data.m_DCReadOnly.System_User_Rules
                         from su in data.m_DCReadOnly.System_Users
                         where su.System_UserKey == _guiUserKey &&
                         sur.System_UserKey == su.System_UserKey 
                         select sur;
            return iQuery;
        }
        
        public static IQueryable GetRuleByUser(Guid _guiUserKey)
        {
            DBMain data = new DBMain();
            var iQuery = from sur in data.m_DCReadOnly.System_User_Rules
                         from su in data.m_DCReadOnly.System_Users
                         from sr in data.m_DCReadOnly.System_Rules
                         where su.System_UserKey == _guiUserKey &&
                         sur.System_UserKey == su.System_UserKey &&
                               sur.System_RuleKey == sr.System_RuleKey

                         select new
                         {
                             sur.System_UserKey,
                             sur.System_RuleKey,
                             sr.RuleID,
                             sr.RuleName,
                             sr.RuleNameVi,
                             sr.ParentID,
                             sr.Level,
                             sur.AllowAccess,
                             sur.AllowAdd,
                             sur.AllowDelete,
                             sur.AllowEdit,
                             sur.AllowExport,
                             sur.AllowImport,
                             sur.AllowPrint,
                             sur.AllowSave
                             
                         };

            return iQuery.OrderBy(p => p.Level);

        }

        //public static IQueryable GetRuleByGroupRemove(DBMain data, Guid _guiUserKey, int _intGroupKey)
        //{
        //    var result = from p1 in data.m_DCReadOnly.System_User_Groups
        //                 from p2 in data.m_DCReadOnly.System_Group_Rules
        //                 where p1.System_GroupKey == p2.System_GroupKey

        //}
    
    }
}
